import re
from datetime import datetime
from builtins import max as mx


def process_log_file_v7(file_path, ignore_log_content):
    seen_logs = set()  # 记录已处理的Lv2和Lv3日志类型
    log_errors = []  # 存储所有去重后的日志错误内容
    storage_errors = []  # 存储所有去重后的硬盘错误内容
    max_input_time = 0  # 跟踪最大的input time值

    with open(file_path, 'r') as file:
        for line in file:
            if any(ignore_content in line for ignore_content in ignore_log_content):
                continue  # 忽略当前行

            match = re.match(r'\[(\d{4}/\d{2}/\d{2}-\d{2}:\d{2}:\d{2})\|(.*?)\|(\d+)\|(TSDK-Lv[23])\](.*)', line)
            if match:
                _, log_type, _, level, message = match.groups()

                # 更新最大的input time值
                input_time_match = re.search(r'input time (\d+)ms', message)
                if input_time_match:
                    input_time = int(input_time_match.group(1))
                    if input_time > max_input_time:
                        max_input_time = input_time

                # 构建唯一标识符用于去重
                log_key = f"{log_type}_{level}"
                if log_key in seen_logs:
                    continue  # 如果已处理过相同类型的日志，则忽略
                seen_logs.add(log_key)

                # 分类存储日志并记录错误信息
                if "storage" in log_type:
                    storage_errors.append(line.strip())  # 添加硬盘错误信息
                else:
                    log_errors.append(line.strip())  # 添加普通日志错误信息

    # 根据是否有错误信息设置结果状态
    log_result = "fail" if log_errors else "pass"
    storage_result = "fail" if storage_errors else "pass"

    # 如果存在最大的input time值，则添加到storage_errors中
    if max_input_time > 0:
        storage_errors.append(f"Max input time: {max_input_time} ms")

    return log_result, storage_result, log_errors, storage_errors


ignore_log_content = [
    'switch next block',
    'Invalid Input:iEventMask:ffffffff',
    'Skip time change',
    'No data break continue',
    'cannot read,0,errno 0 Success',
    'ioctl SIOCSIFADDR wlan0 failed by No such device'
]

# 调用函数处理日志文件
output_file_path_v3 = process_log_file_v7('NQ10-1_03-25.log', ignore_log_content)  # 确保这是正确的路径
print(f"Processed log file saved to: {output_file_path_v3}")
